// pages/newlyAdd/newlyAdd.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    start_time:'开始时间',
    end_time:'结束时间',
    imgSrc: [],
    i: 0,
    hiddenImg: true,
    name:'',
    spec:'',
    pihao:'',
    pinlv:'',
    jiliang:'',
    fangshi:'',
    photo:''
  },
  //删除img
  deleImg: function (e) {
    var that = this
    var index = e.target.dataset.index;
    var images = that.data.imgSrc;
    images.splice(index, 1);
    that.setData({
      imgSrc: images
    });
  },
  addImg: function () {
    var that = this
    wx.chooseImage({
      count: 4, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        if (tempFilePaths) {
          that.setData({
            hiddenImg: false
          })
        }
        that.setData({
          imgSrc: tempFilePaths
        })
      }
    })
  },
  bindDateChange: function (e) {
    this.setData({
      start_time: e.detail.value
    })
  },
  bindDateChange2: function (e) {
    this.setData({
      end_time: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  getName:function (e) {
    var name = e.detail.value;
    this.setData({
      name:name
    });
  },
  getSpec: function (e) {
    var spec = e.detail.value;
    this.setData({
      spec: spec
    });
  },
  getPihao: function (e) {
    var pihao = e.detail.value;
    this.setData({
      pihao: pihao
    });
  },
  getPinlv: function (e) {
    var pinlv = e.detail.value;
    this.setData({
      pinlv: pinlv
    });
  },
  getJiliang: function (e) {
    var jiliang = e.detail.value;
    this.setData({
      jiliang: jiliang
    });
  },
  getFangshi: function (e) {
    var fangshi = e.detail.value;
    this.setData({
      fangshi: fangshi
    });
  },

  send:function (e){
    var that = this;
    var imgSrc = that.data.imgSrc;
    if(imgSrc != ''){
      that.upload();
    }else{
      that.save();
    }
    
  },

  upload: function (e) {
    var that = this;
    var imgSrc = that.data.imgSrc;
    var i = that.data.i;
    var length = imgSrc.length;
    wx.uploadFile({
      url: app.d.ceshiUrl + '/Api/Medicine/uploadimg',
      filePath: imgSrc[i],
      name: 'data',
      success: function (res) {
        console.log('uploadImage success, res is:', res);
        that.setData({
          photo: that.data.photo.concat(res.data) + ',',
          i: that.data.i + 1,
        })
        if (that.data.i < length) {
          that.upload();
        } else {
          that.save();
        }
      },

      fail: function ({ errMsg }) {
        console.log('uploadImage fail, errMsg is', errMsg)
        return false;
      }
    })
  },
  save: function (e) {
    var that = this;
    var imgSrc = that.data.imgSrc;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Medicine/add',
      method: "post",
      data: {
        uid: app.d.userId,
        name: that.data.name,
        photo: that.data.photo,
        spec: that.data.spec,
        pihao: that.data.pihao,
        start_time: that.data.start_time,
        end_time: that.data.end_time,
        pinlv: that.data.pinlv,
        jiliang: that.data.jiliang,
        fangshi: that.data.fangshi,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.status == 1) {
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
          setTimeout(function () {
            wx.navigateBack({

            })
          }, 2000)

        } else {
          wx.showToast({
            title: res.data.err,
            duration: 2000
          })
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 30000
        });
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})